#include "protocol.h"

int main()
{
    int sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
    if (sockfd < 0)
    {
        perror("socket()");
        exit(1);
    }

    struct sockaddr_un addr;

    addr.sun_family = AF_UNIX;
    unlink(CLIENTPATH);
    strcpy(addr.sun_path, CLIENTPATH);

    if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
        perror("bind()");
        exit(1);
    }
    
    struct sockaddr_un dstaddr;
    dstaddr.sun_family = AF_UNIX;
    strcpy(dstaddr.sun_path, SERVERPATH);
    socklen_t len = sizeof(dstaddr);
    char sendbuff[BUFFSIZE] = {0};
    char recvbuff[BUFFSIZE] = {0};
    while (1)
    {
        fgets(sendbuff, BUFFSIZE, stdin);
        sendto(sockfd, sendbuff, strlen(sendbuff), 0, (struct sockaddr *)&dstaddr, len);
        recvfrom(sockfd, recvbuff, BUFFSIZE, 0, (struct sockaddr *)&dstaddr, &len);
        puts(recvbuff);
    }

    close(sockfd);
}
